package com.scwatch.ext.domain;

import com.scwatch.common.web.Anchor;
import com.scwatch.core.domain.MemberGroup;
import com.scwatch.core.domain.Site;
import com.scwatch.core.domain.User;
import com.scwatch.core.support.Commentable;
import com.scwatch.core.support.Siteable;
import org.apache.commons.lang3.StringUtils;

import javax.persistence.*;
import java.util.*;

/**
 * 直播实体类
 */
@Entity
@Table(name = "cms_live_broadcast")
public class LiveBroadcast implements Commentable,Anchor,Siteable {
    private static final long serialVersionUID = 1L;
   /**
    *预约中状态
    */
   private static final int MAKE_AN_APPOINTMENT = 0;
    /**
     *正在直播状态
     */
    private static final int LIVE_BROADCAST = 1;
    /**
     *回看状态
     */
    private static final int LOOK_BACK = 2;
    /**
     *关闭预约
     */
    private static final int CLOSE_SUBSCRIBE = 0;
    /**
     *开启预约
     */
    private static final int OPEN_SUBSCRIBE = 1;
    /**
     *关闭评论
     */
    private static final int CLOSE_REVIEW = 0;
    /**
     *开启评论
     */
    private static final int OPEN_REVIEW = 1;

    @Transient
    public void applyLiveBroadcast() {
        if (getStatus() == null) {
            setStatus(MAKE_AN_APPOINTMENT);
        }
        if (getSubscribe() == null){
            setSubscribe(CLOSE_SUBSCRIBE);
        }
        if (getReview() == null){
            setReview(CLOSE_REVIEW);
        }
    }


    private Integer id;
    private Site site;
    private String title;
    private User user;
    private Date time;
    private String source;
    private Integer status;
    private Integer population;
    private Integer order;
    private Integer subscribe;
    private Integer review;
    private String taddress;
    private String image;
    private String introduction;
    private String fLookBack;
    private String compere;
//    private String importaddress;
//    private String outputaddress;
//    private Integer liveroom;
    private Date createtime;

    private Boolean wowzaType;

    private Integer views; //浏览次数
    private Integer realviews; //真实浏览次数





    private List<LiveBroadcastAddress> livebroadcasts = new ArrayList<LiveBroadcastAddress>(0);

    @Column(name = "wowza_type")
    public Boolean getWowzaType() {
        return wowzaType;
    }

    public void setWowzaType(Boolean wowzaType) {
        this.wowzaType = wowzaType;
    }





    @Id
    @Column(name = "f_live_broadcast_id", unique = true, nullable = false)
    @TableGenerator(name = "tg_cms_live_broadcast", pkColumnValue = "cms_live_broadcast", initialValue = 1, allocationSize = 10)
    @GeneratedValue(strategy = GenerationType.TABLE, generator = "tg_cms_live_broadcast")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    @Column(name = "f_title", nullable = false)
    public String getTitle() {
        return title;
    }

    @Transient
    public String getUrl() {
        return null;
    }

    @Transient
    public Boolean getNewWindow() {
        return null;
    }

    @Transient
    public String getColor() {
        return null;
    }

    @Transient
    public Boolean getStrong() {
        return null;
    }

    @Transient
    public Boolean getEm() {
        return null;
    }

    @Transient
    public String getDescription() {
        return null;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "f_broadcastdate")
    public Date getTime() {
        return time;
    }

    public void setTime(Date time) {
        this.time = time;
    }

    @Column(name = "f_source")
    public String getSource() {
        return source;
    }

    public void setSource(String source) {
        this.source = source;
    }

    @Column(name = "f_status", nullable = false)
    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    @Column(name = "f_subscribe", nullable = false)
    public Integer getSubscribe() {
        return subscribe;
    }

    public void setSubscribe(Integer subscribe) {
        this.subscribe = subscribe;
    }

    @Column(name = "f_order_population")
    public Integer getOrder() {
        return order;
    }

    public void setOrder(Integer order) {
        this.order = order;
    }

    @Column(name = "f_review")
    public Integer getReview() {
        return review;
    }

    public void setReview(Integer review) {
        this.review = review;
    }


    @Column(name = "f_taddress")
    public String getTaddress() {
        return taddress;
    }

    public void setTaddress(String taddress) {
        this.taddress = taddress;
    }

    @Column(name = "f_live_broadcast_image", nullable = true)
    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    @Column(name = "f_introduction")
    public String getIntroduction() {
        return introduction;
    }

    public void setIntroduction(String introduction) {
        this.introduction = introduction;
    }

    @Column(name = "f_look_back")
    public String getfLookBack() {
        return fLookBack;
    }

    public void setfLookBack(String fLookBack) {
        this.fLookBack = fLookBack;
    }


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "f_site_id", nullable = false)
    public Site getSite() {
        return site;
    }

    public void setSite(Site site) {
        this.site = site;
    }

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "f_user_id", nullable = false)
    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    @Column(name = "f_subscribe_population")
    public Integer getPopulation() {
        return population;
    }

    public void setPopulation(Integer population) {
        this.population = population;
    }

    @Column(name = "f_compere")
    public String getCompere() {
        return compere;
    }

    public void setCompere(String compere) {
        this.compere = compere;
    }

//    @Column(name = "f_import_address")
//    public String getImportaddress() {
//        return importaddress;
//    }

//    public void setImportaddress(String importaddress) {
//        this.importaddress = importaddress;
//    }

//    @Column(name = "f_output_address")
//    public String getOutputaddress() {
//        return outputaddress;
//    }
//
//    public void setOutputaddress(String outputaddress) {
//        this.outputaddress = outputaddress;
//    }
//
//    @Column(name = "f_live_broadcast_room")
//    public Integer getLiveroom() {
//        return liveroom;
//    }
//
//    public void setLiveroom(Integer liveroom) {
//        this.liveroom = liveroom;
//    }

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "f_create_time")
    public Date getCreatetime() {
        return createtime;
    }

    public void setCreatetime(Date createtime) {
        this.createtime = createtime;
    }


    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL,orphanRemoval = false, mappedBy = "liveBroadcast")
    public List<LiveBroadcastAddress> getLivebroadcasts() {
        return livebroadcasts;
    }

    public void setLivebroadcasts(List<LiveBroadcastAddress> livebroadcasts) {
        this.livebroadcasts = livebroadcasts;
    }

    @Column(name = "f_views", nullable = false)
    public Integer getViews() {
        return views;
    }

    public void setViews(Integer views) {
        this.views = views;
    }

    @Column(name = "f_real_views", nullable = false)
    public Integer getRealviews() {
        return realviews;
    }

    public void setRealviews(Integer realviews) {
        this.realviews = realviews;
    }

    @Override
    @Transient
    public void addComments(int comments) {

    }

    @Override
    @Transient
    public int getCommentStatus(User user, Collection<MemberGroup> groups) {
        return 0;
    }

    @Transient
    public String getJsonUrl() {
        String filename="/lives/{year}/{month}/{day}/"+getId()+".json";
        Calendar now = Calendar.getInstance();
        now.setTime(getCreatetime());
        String year = String.valueOf(now.get(Calendar.YEAR));
        int m = now.get(Calendar.MONTH) + 1;
        int d = now.get(Calendar.DAY_OF_MONTH);
        String month = StringUtils.leftPad(String.valueOf(m), 2, '0');
        String day = StringUtils.leftPad(String.valueOf(d), 2, '0');
        filename = StringUtils.replace(filename, "{year}", year);
        filename = StringUtils.replace(filename, "{month}", month);
        filename = StringUtils.replace(filename, "{day}", day);
        return  filename;
    }

    @Transient
    public String getSharedUrl() {
        String sharedFilename=getJsonUrl();
        sharedFilename=sharedFilename.replaceFirst(".json", "_shared.html");
        return  sharedFilename;
    }

}
